package com.wiloon.java8x.io;

/*
  Created by roy.wang on 2017/4/20;
 */

import java.io.*;
import java.net.URISyntaxException;
import java.net.URL;

public class BufferedStreamDemo {
    public static void main(String[] args) throws URISyntaxException {
        try {
            byte[] data = new byte[1024];
            URL from = ClassLoader.getSystemClassLoader().getResource("foo.txt");
            File srcFile = new File(from.toURI());
            URL to = ClassLoader.getSystemClassLoader().getResource("bar.txt");
            File desFile = new File(to.toURI());
            BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(srcFile));
            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(desFile));
            System.out.println("复制文件：" + srcFile.length() + "字节");
            while (bufferedInputStream.read(data) != -1) {
                bufferedOutputStream.write(data);
            }

            // 将缓冲区中的数据全部写出
            bufferedOutputStream.flush();
            // 关闭流
            bufferedInputStream.close();
            bufferedOutputStream.close();
            System.out.println("复制完成");
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("using: java UseFileStream src des");
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}